'''
Created on 10.11.2011

@author: halldor
'''
from datetime import date
from TimeUtils.utils import *
from Calendar.Iceland import Iceland
from TimeUtils.daterolling import DateRollingConvention
from TimeUtils.daycount import *
from TimeUtils.utils import *
from IRUtils import *
from math import log

class InterestRateGenre(object):
    '''
    Enum class for interest rate genre
    Parameters: 
        flatrate
        bondyield
        continuousyield
        swaprate
    Returns:
    '''
    (flatrate, bondyield, continuousyield) = range(3)

class InterestRate(object):
    def __init__(self):
        pass
    def discountfactor(self):
        pass
    def yearfrac(self):
        pass

class MoneyMarketRate(InterestRate):
    def __init__(self, valuedate, maturitydate, rate, daycountbasis, cal):
        self.valuedate = valuedate
        self.maturitydate = maturitydate
        self.rate = rate
        self.daycountbasis = daycountbasis

    def discountfactor(self):
        date1yfwd =     addmonth(self.valuedate, 12)
        actualdays =    float(daysbetween(self.valuedate, date1yfwd, DayCountBasis().basis_actact))
        days =          float(daysbetween(self.valuedate, self.maturitydate, self.daycountbasis))

        if self.daycountbasis == DayCountBasis().basis_act360:
            basisdays = 360.0
        elif self.daycountbasis == DayCountBasis().basis_30360:
            basisdays = 360.0
        elif self.daycountbasis == DayCountBasis().basis_act365:
            basisdays = 365.0
        else:
            basisdays = actualdays
        
        df = 1 / (1+self.rate*days/basisdays)
        return df

    def continuousrate(self,):
        date1yfwd =     addmonth(self.valuedate, 12)
        actualdays =    float(daysbetween(self.valuedate, date1yfwd, DayCountBasis().basis_actact))
        days =          float(daysbetween(self.valuedate, self.maturitydate, self.daycountbasis))

        if self.daycountbasis == DayCountBasis().basis_act360:
            basisdays = 360.0
        elif self.daycountbasis == DayCountBasis().basis_30360:
            basisdays = 360.0
        elif self.daycountbasis == DayCountBasis().basis_act365:
            basisdays = 365.0
        else:
            basisdays = actualdays
        
        continuousrate = actualdays/days * log(1+self.rate*days/basisdays)
        return continuousrate
    
    def yearfrac(self):
        yf = yearfraction(self.valuedate, self.maturitydate, DayCountBasis().basis_actact)
        return yf